part of 'main.dart';

// The callback function should always be a top-level or static function.
@pragma('vm:entry-point')
void startCallback() {
  // FlutterForegroundTask.setTaskHandler(MyTaskHandler());
}

class MyTaskHandler {
  // Called when the task is started.
  @override
  Future<void> onStart(DateTime timestamp, SendPort? starter) async {
    print('onStart(starter: ${starter.toString()})');
  }

  // Called based on the eventAction set in ForegroundTaskOptions.
  @override
  void onRepeatEvent(DateTime timestamp, SendPort? sendPort) {
    // Send data to main isolate.
    final Map<String, dynamic> data = {
      "timestampMillis": timestamp.millisecondsSinceEpoch,
    };
    // FlutterForegroundTask.sendDataToMain(data);
  }

  // Called when the task is destroyed.
  @override
  Future<void> onDestroy(DateTime timestamp, SendPort? sendPort) async {
    print('onDestroy');
  }

  // Called when data is sent using `FlutterForegroundTask.sendDataToTask`.
  @override
  void onReceiveData(Object data) {
    print('onReceiveData: $data');
  }

  // Called when the notification button is pressed.
  @override
  void onNotificationButtonPressed(String id) {
    print('onNotificationButtonPressed: $id');
  }

  // Called when the notification itself is pressed.
  @override
  void onNotificationPressed() {
    print('onNotificationPressed');
  }

  // Called when the notification itself is dismissed.
  @override
  void onNotificationDismissed() {
    print('onNotificationDismissed');
  }
}

class ServiceManager {
  static final ServiceManager _instance = ServiceManager._internal();

  factory ServiceManager() {
    return _instance;
  }

  ServiceManager._internal();

  Future<void> _requestPermissions() async {
    // Android 13+, you need to allow notification permission to display foreground service notification.
    //
    // iOS: If you need notification, ask for permission.
    // final NotificationPermission notificationPermission =
    // await FlutterForegroundTask.checkNotificationPermission();
    // if (notificationPermission != NotificationPermission.granted) {
    //   await FlutterForegroundTask.requestNotificationPermission();
    // }
  }

  void _initService() {
    if (Platform.isMacOS || Platform.isWindows) {
      return;
    }
    // FlutterForegroundTask.init(
    //   androidNotificationOptions: AndroidNotificationOptions(
    //     channelId: 'foreground_service',
    //     channelName: 'Foreground Service Notification',
    //     channelDescription:
    //     'This notification appears when the foreground service is running.',
    //     // onlyAlertOnce: true,
    //   ),
    //   iosNotificationOptions: const IOSNotificationOptions(
    //     showNotification: false,
    //     playSound: false,
    //   ),
    //   foregroundTaskOptions: ForegroundTaskOptions(
    //     // eventAction: ForegroundTaskEventAction.repeat(5000),
    //     autoRunOnBoot: true,
    //     autoRunOnMyPackageReplaced: true,
    //     allowWakeLock: true,
    //     allowWifiLock: true,
    //   ),
    // );
  }

  Future<bool> startService() async {
    debugPrint("ServiceManager startService");
    // if (await FlutterForegroundTask.isRunningService) {
    //   return FlutterForegroundTask.restartService();
    // } else {
    //   return FlutterForegroundTask.startService(
    //     notificationTitle: 'Foreground Service is running',
    //     notificationText: 'Tap to return to the app',
    //     // serviceId: 256,
    //     // notificationIcon: null,
    //     // notificationButtons: [
    //     //   const NotificationButton(id: 'btn_hello', text: 'hello'),
    //     // ],
    //     // notificationInitialRoute: '/',
    //     callback: startCallback,
    //   );
    // }
    return false;
  }

  Future<bool> stopService() async {
    return false;//FlutterForegroundTask.stopService();
  }
}

